package com.hw.paltfrom.service.system;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.hw.paltfrom.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.mapper.HwbaseDataItemDetailMapper;
import com.hw.paltfrom.mapper.HwbaseDataItemMapper;
import com.hw.paltfrom.pojo.HwBaseDataItem;
import com.hw.paltfrom.pojo.HwBaseDataItemDetail;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.vo.BaseDataItemDetailVo;

/**
 * 字典明细
 * 邢家华
 * 2019-11-6 16:12:08
 * 
 * @author Administrator
 *
 */
@Service
public class BaseDataltemDetailService {

	@Autowired
	private HwbaseDataItemDetailMapper hwbaseDataItemDetailMapper;
	
	@Autowired
	private HwbaseDataItemMapper dataItemMapper;
	
	@Value("${timerTask}")
	private String timerTask;

	// 字典明细查询
	public Map<String, Object> queryAllList(BaseDataItemDetailVo vo) {
		String fItemcode = vo.getfItemcode();
		String fItemname = vo.getfItemname();
		Integer pageNum = vo.getPageNum();
		Integer pageSize = vo.getPageSize();
		String getfItemid = vo.getfItemid();
		PageHelper.startPage(pageNum, pageSize);
		// 模糊条件
		Wrapper<HwBaseDataItemDetail> namewrapper = new EntityWrapper<HwBaseDataItemDetail>();
		
		if(StringUtils.isNotBlank(getfItemid)){
			namewrapper.eq("F_ItemId", getfItemid);
		}
		
		if (StringUtils.isNotBlank(fItemcode)) {
			namewrapper.like("F_ItemCode", fItemcode).and().eq("F_DeleteMark", "0").orderBy("F_SortCode");
		} else if (StringUtils.isNotBlank(fItemname)) {
			namewrapper.like("F_ItemName", fItemname).and().eq("F_DeleteMark", "0").orderBy("F_SortCode");
		} else if (!StringUtils.isNotBlank(fItemcode) && !StringUtils.isNotBlank(fItemname)) {
			namewrapper.eq("F_DeleteMark", "0").orderBy("F_SortCode");
		}
		List<HwBaseDataItemDetail> hwDataOverrunstationHistoryList = hwbaseDataItemDetailMapper.selectList(namewrapper);
		PageInfo<HwBaseDataItemDetail> info = new PageInfo<HwBaseDataItemDetail>(hwDataOverrunstationHistoryList);
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
		

	}

	// 根据字典查询
	public Map<String, Object> queryParentIdList(BaseDataItemDetailVo vo) {
		
//		分类id
		String itemid = vo.getfItemid();
		
		String getfItemname = vo.getfItemname();
		String getfItemvalue = vo.getfItemvalue();
		Wrapper<HwBaseDataItemDetail> namewrapper = new EntityWrapper<HwBaseDataItemDetail>();
		if (StringUtils.isNotBlank(itemid)) {
			namewrapper.eq("F_ItemId", itemid);
		}
		if (StringUtils.isNotBlank(getfItemname)) {
			namewrapper.eq("F_ItemName", getfItemname);
		}
		if (StringUtils.isNotBlank(getfItemvalue)) {
			namewrapper.eq("F_ItemValue", getfItemvalue);
		}
		
		
		namewrapper.eq("F_DeleteMark", "0").orderBy("F_SortCode");
		List<HwBaseDataItemDetail> hwBaseDataItemDetailList = hwbaseDataItemDetailMapper.selectList(namewrapper);
		JSONArray array = JSONArray.parseArray(JSON.toJSONString(hwBaseDataItemDetailList));
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, array);
	}

	// 新增
	public Map<String, Object> insert(BaseDataItemDetailVo vo) {

		String Createusername = vo.getfCreateusername();
		String Createuserid = vo.getfCreateuserid();
		String fItemid = vo.getfItemid();
		String fItemvalue = vo.getfItemvalue();
		String fItemname = vo.getfItemname();
//		String fItemcode = vo.getfItemcode();
		String fDescription = vo.getfDescription();
		String fParentid = vo.getfParentid();
		if (fParentid == "") {
			fParentid = "0";
		}
		String fSimplespelling = LanguageUtil.getAllFirstLetter(vo.getfItemname());
		Integer fSortcode = vo.getfSortcode();

		// 先判断字典名称是否重复
		Wrapper<HwBaseDataItemDetail> namewrapper = new EntityWrapper<HwBaseDataItemDetail>();
		namewrapper.eq("F_ItemName", fItemname);
		List<HwBaseDataItemDetail> hwBaseDataItemDetailList = hwbaseDataItemDetailMapper.selectList(namewrapper);
		if (!hwBaseDataItemDetailList.isEmpty()) {
			return ResponeResultUtil.fail(HWSystemCode.DATAITEMNAME_EXIT);
		}

//		先判断编码是否重复
//		Wrapper<HwBaseDataItemDetail> codewrapper = new EntityWrapper<HwBaseDataItemDetail>();
//		codewrapper.eq("F_ItemCode", fItemcode).orderBy("F_SortCode");
//		List<HwBaseDataItemDetail> codeList = hwbaseDataItemDetailMapper.selectList(codewrapper);
//		if (!codeList.isEmpty()) {
//			return ResponeResultUtil.fail(HWSystemCode.CODE_EXIT);
//		}
		// 生成ID、创建时间、插入数据库
		HwBaseDataItemDetail hwBaseDataItemDetail = new HwBaseDataItemDetail();
		hwBaseDataItemDetail.setfItemdetailid(RandomNumberUtil.getNumber());
		hwBaseDataItemDetail.setfItemname(fItemname);
		hwBaseDataItemDetail.setfItemid(fItemid);
		hwBaseDataItemDetail.setfItemvalue(fItemvalue);
		hwBaseDataItemDetail.setfItemcode(RandomNumberUtil.getCode());
		hwBaseDataItemDetail.setfCreateuserid(Createuserid);
		hwBaseDataItemDetail.setfCreateusername(Createusername);
		hwBaseDataItemDetail.setfCreatedate(new Timestamp(System.currentTimeMillis()));
		hwBaseDataItemDetail.setfDeletemark(0);
		hwBaseDataItemDetail.setfEnabledmark(1);
		hwBaseDataItemDetail.setfDescription(fDescription);
		hwBaseDataItemDetail.setfParentid(fParentid);
		hwBaseDataItemDetail.setfQuickquery(LanguageUtil.toHanyuPinyin(vo.getfItemname()));
		hwBaseDataItemDetail.setfSimplespelling(fSimplespelling);
		hwBaseDataItemDetail.setfSortcode(fSortcode);
		Integer insert = hwbaseDataItemDetailMapper.insert(hwBaseDataItemDetail);
		if (insert != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}

	// 编辑主键ID查询
	public Map<String, Object> queryFItemdeTailId(String fItemdetailid) {
		HwBaseDataItemDetail hwBaseDataItemDetail = hwbaseDataItemDetailMapper.selectById(fItemdetailid);
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, hwBaseDataItemDetail);
	}

	// 更新或删除
	public Map<String, Object> update(BaseDataItemDetailVo vo) {

		String fItemid = vo.getfItemid();
		String fItemdetailid = vo.getfItemdetailid();
		String fParentid = vo.getfParentid();
		String fItemvalue = vo.getfItemvalue();
		String fItemname = vo.getfItemname();
		Integer fEnabledmark = vo.getfEnabledmark();
		Integer fDeletemark = vo.getfDeletemark();
		String fDescription = vo.getfDescription();
		String fModifyuserid = vo.getfModifyuserid();
		String fModifyusername = vo.getfModifyusername();
		Integer getfSortcode = vo.getfSortcode();
		Wrapper<HwBaseDataItemDetail> wrapper = new EntityWrapper<HwBaseDataItemDetail>();
		wrapper.eq("F_ItemDetailId", fItemdetailid);
		HwBaseDataItemDetail hwBaseDataItemDetail = new HwBaseDataItemDetail();
		
//		判断是否是定时任务相关的code
//		boolean isTask = taskCode(vo.getfItemcode());
		
		// 如果删除标记为1的话，当前是删除操作
		if (vo.getfDeletemark() == 1) {
//			禁止删除定时任务数据字典
//			if(isTask){
//				return ResponeResultUtil.fail(HWSystemCode.BAN_UPDATE);
//			}
			
			
			// 删除操作
			if (fParentid.equals("0")) {
				// 删父级下的子集
				Wrapper<HwBaseDataItemDetail> namewrapper = new EntityWrapper<HwBaseDataItemDetail>();
				namewrapper.eq("F_ParentId", fItemdetailid).and().eq("F_DeleteMark", "0");
				List<HwBaseDataItemDetail> hwBaseDataItemDetailList = hwbaseDataItemDetailMapper
						.selectList(namewrapper);
				for (int i = 0; i < hwBaseDataItemDetailList.size(); i++) {
					Wrapper<HwBaseDataItemDetail> wrapperfParentid = new EntityWrapper<HwBaseDataItemDetail>();
					wrapperfParentid.eq("F_ItemDetailId", hwBaseDataItemDetailList.get(i).getfItemdetailid());
					hwBaseDataItemDetail.setfDeletemark(fDeletemark);
					hwBaseDataItemDetail.setfModifydate(new Timestamp(System.currentTimeMillis()));
					hwBaseDataItemDetail.setfModifyuserid(fModifyuserid);
					hwBaseDataItemDetail.setfModifyusername(fModifyusername);
					Integer update = hwbaseDataItemDetailMapper.update(hwBaseDataItemDetail, wrapperfParentid);
					if (update != 1) {
						return ResponeResultUtil.fail(HWSystemCode.FAIL);
					}
				}
//				删父级自身有另外的接口
				HwBaseDataItemDetail paren = new HwBaseDataItemDetail();
				paren.setfDeletemark(1);
				Integer update = hwbaseDataItemDetailMapper.update(paren, wrapper);
				if (update != 1) {
					return ResponeResultUtil.fail(HWSystemCode.FAIL);
				}
			} else {
				// 删子集
				hwBaseDataItemDetail.setfDeletemark(fDeletemark);
				hwBaseDataItemDetail.setfModifydate(new Timestamp(System.currentTimeMillis()));
				hwBaseDataItemDetail.setfModifyuserid(fModifyuserid);
				hwBaseDataItemDetail.setfModifyusername(fModifyusername);
				Integer update = hwbaseDataItemDetailMapper.update(hwBaseDataItemDetail, wrapper);
				if (update != 1) {
					return ResponeResultUtil.fail(HWSystemCode.FAIL);
				}
			}

		} else {
			// 更新
			Wrapper<HwBaseDataItemDetail> namewrapper = new EntityWrapper<HwBaseDataItemDetail>();
			namewrapper.eq("F_ItemName", fItemname).and().notIn("F_ItemDetailId", fItemdetailid);
			List<HwBaseDataItemDetail> hwBaseDataItemDetailList = hwbaseDataItemDetailMapper.selectList(namewrapper);
			if (!hwBaseDataItemDetailList.isEmpty()) {
				return ResponeResultUtil.fail(HWSystemCode.DATAITEMNAME_EXIT);
			}
			
			hwBaseDataItemDetail.setfItemdetailid(fItemdetailid);
			hwBaseDataItemDetail.setfItemname(fItemname);
			hwBaseDataItemDetail.setfItemid(fItemid);
			hwBaseDataItemDetail.setfItemvalue(fItemvalue);
			hwBaseDataItemDetail.setfDeletemark(fDeletemark);
			hwBaseDataItemDetail.setfDescription(fDescription);
			hwBaseDataItemDetail.setfEnabledmark(fEnabledmark);
			hwBaseDataItemDetail.setfModifydate(new Timestamp(System.currentTimeMillis()));
			hwBaseDataItemDetail.setfModifyuserid(fModifyuserid);
			hwBaseDataItemDetail.setfModifyusername(fModifyusername);
			hwBaseDataItemDetail.setfSortcode(getfSortcode);
			Integer update = hwbaseDataItemDetailMapper.update(hwBaseDataItemDetail, wrapper);
			if (update != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
//			if(isTask){
//				RedisUtil redis = SpringUtil.getBean(RedisUtil.class);
//				redis.set(vo.getfItemcode(), fItemvalue);
//			}
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}

//	private boolean taskCode(String name){
//		String tmp=new String(timerTask.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
//		return tmp.contains(name);
//	}
	
	
	public Map<String, Object> sortList() {
		
//		HashMap<String, List<String>> result = new HashMap<String, List<String>>();
		
		ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
		
//		查所有一级字典
		Wrapper<HwBaseDataItemDetail> namewrapper = new EntityWrapper<HwBaseDataItemDetail>();
		namewrapper.eq("F_ParentId", "0").and().eq("F_DeleteMark", "0");
		List<HwBaseDataItemDetail> selectList = hwbaseDataItemDetailMapper.selectList(namewrapper);
		
//		遍历父类找出子类
		for(HwBaseDataItemDetail did:selectList){
			HashMap<String, Object> Parent = new HashMap<String, Object>();
			Wrapper<HwBaseDataItemDetail> wrapper = new EntityWrapper<HwBaseDataItemDetail>();
			wrapper.eq("F_ParentId", did.getfItemdetailid()).and().eq("F_DeleteMark", "0");
			List<HwBaseDataItemDetail> list = hwbaseDataItemDetailMapper.selectList(wrapper);
			
			
		  ArrayList<Object> someone = new ArrayList<>();
			for(HwBaseDataItemDetail d:list){
				HashMap<String, Object> son = new HashMap<String, Object>();
				son.put("id", d.getfItemdetailid());
				son.put("title", d.getfItemname());
				someone.add(son);
			}
			
			Parent.put("id", did.getfItemdetailid());
			Parent.put("title", did.getfItemname());
//			if(!son.isEmpty()){
				Parent.put("children", someone);
//			}
			result.add(Parent);
		}
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS,result);
		
	}
	
	public Map<String, Object> itemDataSort(){

		
//		HashMap<String, List<String>> result = new HashMap<String, List<String>>();
		
		ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
		
//		查所有一级字典
		Wrapper<HwBaseDataItem> namewrapper = new EntityWrapper<HwBaseDataItem>();
		namewrapper.eq("F_ParentId", "0").eq("F_DeleteMark", "0").orderBy("F_SortCode");
		List<HwBaseDataItem> selectList = dataItemMapper.selectList(namewrapper);
		
//		遍历父类找出子类
		for(HwBaseDataItem did:selectList){
			HashMap<String, Object> Parent = new HashMap<String, Object>();
			Wrapper<HwBaseDataItem> wrapper = new EntityWrapper<HwBaseDataItem>();
			wrapper.eq("F_ParentId", did.getfItemid()).eq("F_DeleteMark", "0").orderBy("F_SortCode");
			List<HwBaseDataItem> list = dataItemMapper.selectList(wrapper);
			
			
		  ArrayList<Object> someone = new ArrayList<>();
			for(HwBaseDataItem d:list){
				HashMap<String, Object> son = new HashMap<String, Object>();
				son.put("id", d.getfItemid());
				son.put("title", d.getfItemname());
				someone.add(son);
			}
			
			Parent.put("id", did.getfItemid());
			Parent.put("title", did.getfItemname());
//			if(!son.isEmpty()){
				Parent.put("children", someone);
//			}
			result.add(Parent);
		}
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS,result);
		
	
		
		
		
		
		
		
		
		
		
		
	}
}
